import Link from "@docusaurus/Link";
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import HomepageFeatures from "@site/src/components/HomepageFeatures";
import Heading from "@theme/Heading";
import Layout from "@theme/Layout";
import clsx from "clsx";
import { getUserList } from "../server/api";

import { useEffect, useState } from "react";
import styles from "./index.module.css";

function HomepageHeader() {
  const { siteConfig } = useDocusaurusContext();
  const [user, setUser] = useState<
    { age: number; gender: string; name: string }[]
  >([]);

  useEffect(() => {
    getUserList().then((res) => {
      if (res.code === 0) {
        setUser(res.data);
      }
    });
  }, []);

  return (
    <header className={clsx("hero hero--primary", styles.heroBanner)}>
      <div className="container">
        <Heading as="h1">{siteConfig.title}</Heading>
        <p className="hero__subtitle">{siteConfig.tagline}</p>
        <div className={styles.buttons}>
          <Link
            className="button button--secondary button--lg"
            to="/docs/intro"
          >
            Docusaurus Tutorial - 5min ⏱️
          </Link>
        </div>
        <div>用户列表</div>
        <div>
          {user.map((u, i) => {
            return (
              <div
                key={i}
                style={{ display: "flex", justifyContent: "center" }}
              >
                <div>姓名：{u.name}</div>
                <div>性别：{u.gender}</div>
                <div>年龄：{u.age}</div>
              </div>
            );
          })}
        </div>
      </div>
    </header>
  );
}

export default function Home(): JSX.Element {
  const { siteConfig } = useDocusaurusContext();
  return (
    <Layout
      title={`Hello from ${siteConfig.title}`}
      description="Description will go into a meta tag in <head />"
    >
      <HomepageHeader />
      <main>
        <HomepageFeatures />
      </main>
    </Layout>
  );
}
